import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jetour_line/aha_jetour_line.dart';
import 'package:jetour_line/base_widget/base_scaffold.dart';
import 'package:jetour_line/my_colors.dart';
import 'package:jetour_line/utils/aha_utils.dart';
import 'package:jetour_line/utils/extension_int.dart';
import 'package:jetour_line/utils/page_push_utils.dart';

import '../../base_widget/base_image.dart';
import '../../base_widget/dash_line.dart';
import '../../base_widget/image_network_widget.dart';
import '../../base_widget/my_alert.dart';
import '../../models/jetour_line_detail.dart';
import '../../network/jetour_http.dart';
import 'edit_node_page.dart';
import 'end_travel_page.dart';

typedef _ChangeDayCallBack = void Function(int day);
typedef _DelCallBack = void Function(int groupIndex,int childIndex);

class CreatTravelListWidget extends StatefulWidget {
  final JetourDetailModel? detailModel;
  final int? lineId;
  final _ChangeDayCallBack callBack;
  final _DelCallBack delcallBack;
  CreatTravelListWidget(
      {Key? key, required this.detailModel, required this.lineId, required this.callBack, required this.delcallBack,})
      : super(key: key);

  @override
  State<CreatTravelListWidget> createState() => _CreatTravelListWidgetState();
}

class _CreatTravelListWidgetState extends State<CreatTravelListWidget> {
  int selIndex = 0; //当前选择的天数
  int dayCount = 0; //天数
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }
  BuildContext? mContext;
  @override
  Widget build(BuildContext context) {
    mContext = context;
    if(widget.detailModel == null){
      dayCount = 1;
    }else
    if (widget.detailModel!.nodeList.length <= 1) {
      dayCount = 1;
    } else {
      dayCount = widget.detailModel!.nodeList.length;
    }
    return Expanded(
      flex: 4,
      child: Column(
        children: [
          titleWidget(),
          if(widget.detailModel != null)
          listWidget(selIndex),
        ],
      ),
    );
  }

  Widget titleWidget() {
    return Container(
      width: double.infinity,
      height: 96.px,
      color: MyColors.colorF1F1F1,
      child: Row(
        children: [
          GestureDetector(
            onTap: () {
              // selIndex = -1;
              // setState(() {
              //
              // });
            },
            child: Container(
              color: selIndex == -1 ? MyColors.white : Colors.transparent,
              alignment: Alignment.center,
              width: 110.px,
              height: 96.px,
              child: Text(
                '全程',
                style: TextStyle(
                    fontSize: 30.px,
                    color: selIndex == -1 ? MyColors.color00A198 : MyColors.color999999),
              ),
            ),
          ),
          Expanded(
            child: ListView.builder(
              itemCount: dayCount, itemBuilder: (context, index) {
              return titleItemWidget(index);
            }, scrollDirection: Axis.horizontal,),
          ),
          GestureDetector(onTap: () {
            String? showTxt;
            NodeList node = widget.detailModel!.nodeList.last;
            if (node.node.isEmpty) {
              showTxt = '请先完善第${widget.detailModel!.nodeList.length}天信息';
              AhaUtils.showTxt('请先完善已有行程');
            }
            if (showTxt == null) {
              setState(() {
                widget.detailModel!.nodeList.add(NodeList(dayId: 0,
                    startCity: '',
                    endCity: '',
                    distance: 0,
                    distanceText: '',
                    node: []));
                dayCount = dayCount + 1;
                selIndex = dayCount - 1;
              });
              widget.callBack(selIndex);
            }
          }, child: Container(
            margin: EdgeInsets.only(right: 23.px),
            width: 64.px,
            height: 64.px,
            color: Colors.white,
            alignment: Alignment.center,
            child: BaseImage(
              image: 'lsgj_btn_add',
              width: 40.px,
              height: 40.px,
            ),
          ),)

        ],
      ),
    );
  }

  Widget titleItemWidget(int index) {
    bool isSel = selIndex == index;
    String title;
    title = 'D${index + 1}';
    return GestureDetector(
      onTap: () {
        setState(() {
          selIndex = index;
        });
        widget.callBack(selIndex);
      },
      child: Container(
        color: isSel ? MyColors.white : Colors.transparent,
        alignment: Alignment.center,
        width: 110.px,
        height: 96.px,
        child: Text(
          title,
          style: TextStyle(
              fontSize: 30.px,
              color: isSel ? MyColors.color00A198 : MyColors.color999999),
        ),
      ),
    );
  }


  Widget listWidget(int index) {
    bool isAll = false;
    if (selIndex == -1) {
      isAll = true;
    }
    return Expanded(child: ListView(children: [
      isAll ? Column(children: [
        for(int i = 0; i < widget.detailModel!.nodeList.length; i++)
          listItemWidget(i)
      ],) : listItemWidget(selIndex)

    ],));
  }

  Widget listItemWidget(int index) {

    NodeList? nodeList;
    if (widget.detailModel!= null && widget.detailModel!.nodeList.isEmpty) {
      nodeList = NodeList(dayId: 0,
          startCity: 'startCity',
          endCity: 'endCity',
          distance: 0,
          distanceText: 'distanceText',
          node: []);
    } else {
      nodeList = widget.detailModel == null ? null: widget.detailModel!.nodeList[index];
    }
    return Column(children: [
      Row(children: [
        SizedBox(width: 32.px,),
        if( nodeList!.node.isNotEmpty)
          Text('${nodeList.startCity}-${nodeList.endCity}', style: TextStyle(
              fontSize: 30.px,
              color: MyColors.color1A1A1A,
              fontWeight: FontWeight.w500),),
        SizedBox(width: 20.px, height: 94.px,),
        if( nodeList.node.isNotEmpty)
          Text('约${nodeList.distanceText}',
            style: TextStyle(fontSize: 24.px, color: MyColors.color999999),),
        const Expanded(child: SizedBox(),),
        if(nodeList.node.isNotEmpty)
        GestureDetector(
          onTap: () { //删除天
            if (nodeList!.node.isEmpty) {
              selIndex = selIndex - 1;
              widget.detailModel!.nodeList.removeAt(index);
              setState(() {

              });
              widget.callBack(selIndex);
            } else {
              delDay(nodeList.dayId);
            }
          },
          child: Container(
            width: 96.px,
            height: 96.px,
            color: Colors.transparent,
            alignment: Alignment.center,
            child: BaseImage(
              image: 'lsgj_btn_delete',
              width: 36.px,
              height: 36.px,
            ),

          ),
        )
      ],),
      for(int i = 0; i < nodeList.node.length; i++)
        getChildItemWidget(index, i)
    ],);
  }


  Widget getChildItemWidget(int groupIndex, int childIndex) {
    NodeList nodeList = widget.detailModel!.nodeList[groupIndex];
    Node node = nodeList.node[childIndex];
    String num = '${childIndex + 1}';
    String? img  = node.logo;
    String title = node.caption;
    String subTitle = node.address;
    return Container(
      padding: EdgeInsets.only(left: 25.px, right: 20.px),
      color: Colors.transparent,
      width: double.infinity,
      height: 144.px,
      child: Row(
        children: [
          Column(
            children: [
              Expanded(
                  child: Opacity(
                    opacity: childIndex == 0 ? 0 : 1,
                    child: DasheLine(
                      axis: Axis.vertical,
                      dashedWidth: 1,
                      dashedColor: MyColors.colorF1F1F1,
                    ),
                  )),
              ClipRRect(
                borderRadius: BorderRadius.all(Radius.circular(20.px)),
                child: Container(
                  alignment: Alignment.center,
                  width: 40.px,
                  height: 40.px,
                  color: MyColors.colorF1F1F1,
                  child: Text(
                    num,
                    style:
                    TextStyle(fontSize: 22.px, color: MyColors.color666666),
                  ),
                ),
              ),
              Expanded(
                  child: DasheLine(
                    axis: Axis.vertical,
                    dashedWidth: 1,
                    dashedColor: MyColors.colorF1F1F1,
                  )),
            ],
          ),
          Expanded(
            child: Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(4.px)),
                border: Border.all(width: 1.0, color: MyColors.colorF1F1F1),
              ),
              margin: EdgeInsets.only(left: 35.px, top: 10.px, bottom: 10.px),
              child: Row(
                children: [
                  if (img != null)
                    Padding(
                        padding: EdgeInsets.only(left: 20.px),
                        child: ImageNetworkWidget(
                          img,
                          width: 120.px,
                          height: 84.px,
                        )),
                  Expanded(
                    child: Padding(
                      padding: EdgeInsets.only(left: 20.px),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            title,
                            style: TextStyle(
                                fontSize: 28.px, color: MyColors.color333333),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                          SizedBox(
                            height: 8.px,
                          ),
                          Text(
                            subTitle,
                            style: TextStyle(
                                fontSize: 24.px, color: MyColors.color999999),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ],
                      ),
                    ),
                  ),
                  GestureDetector(
                    onTap: () {
                      NodeList nodeList = widget.detailModel!.nodeList[groupIndex];
                      Node node = nodeList.node[childIndex];
                      PagePushUtils.push(context, EditNodeScreen(id: widget.lineId!,nodeId: node.nodeId,nodeInfo: node, callBack: () {
                        setState(() {
                          nodeList.node.removeAt(childIndex);
                        });
                        widget.delcallBack(groupIndex,childIndex);
                      },)).then((value) {
                        widget.detailModel!.nodeList[groupIndex].node[childIndex] = value as Node;
                        setState(() {

                        });
                      });
                    },
                    child: Container(
                      width: 96.px,
                      height: 96.px,
                      color: Colors.transparent,
                      alignment: Alignment.center,
                      child: BaseImage(
                        image: 'lsgj_btn_edit',
                        width: 36.px,
                        height: 36.px,
                      ),
                    ),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  void delDay(int day) {
    showMyDialog(mContext, '是否删除该天数据', "是", "否", (index) {
      if (index == 0) {
        Map<String, dynamic> map = Map();
        map['id'] = widget.lineId;
        map['day'] = day;
        JetourHttp().post(JetourRouter.GuideToolDeleteDay, data: map).then((
            value) {
          if(selIndex > 0){
            selIndex = selIndex - 1;
          }
          widget.detailModel!.nodeList.removeAt(day - 1);
          widget.callBack(selIndex);
          setState(() {

          });

          widget.delcallBack(day,-1);
        });
      }
    });
  }

}
